package com.onland.dfish.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.onland.dfish.entity.Sort;
import com.onland.dfish.entity.Users;
import com.onland.dfish.entity.Videos;
import com.onland.dfish.mapper.SortMapper;
import com.onland.dfish.mapper.VideoMapper;
import com.onland.dfish.service.IVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("videoService")
public class VideoServiceImpl implements IVideoService {
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private SortMapper sortMapper;

    /**
     * 根据id查询所有的观看历史
     *
     * @param userId 用户id
     * @return 返回观看历史数组
     */
    @Override
    public List<Videos> queryHistory(int userId) {
        return videoMapper.queryHistory(userId);
    }

    /**
     * 查询用户上传的所有视频
     *
     * @param userId 用户id
     * @return 返回视频数组
     */
    @Override
    public List<Videos> queryMyVideos(int userId) {
        return videoMapper.queryMyVideos(userId);
    }

    /**
     * 查询用户收藏的所有视频
     *
     * @param userId 用户id
     * @return 返回视频数组
     */
    @Override
    public List<Videos> queryMyCollect(int userId) {
        return videoMapper.queryMyCollect(userId);
    }


    @Override
    public int createVideo(Videos v) {
        v.setAddTime(new Date());
        return videoMapper.insert(v);
    }

    @Override
    public List<Videos> queryByType(int style1Id, int style2Id) {
        QueryWrapper<Videos> wrapper = new QueryWrapper<>();
        if (0 != style1Id) {
            wrapper.eq("style1_id", style1Id);
        }

        if (0 != style2Id) {
            wrapper.eq("style2_id", style2Id);
        }
        return videoMapper.selectList(wrapper);
    }

    /**
     * 根据关键字查询
     *
     * @param kd
     * @return
     */
    @Override
    public List<Videos> queryByKd(String kd) {
        QueryWrapper<Videos> wrapper = new QueryWrapper<>();
        wrapper.like("title", kd);
        return videoMapper.selectList(wrapper);
    }

    /**
     * 根据Id删除视频
     *
     * @param id
     * @return
     */
    @Override
    public int deleteById(int id) {
        return videoMapper.deleteById(id);
    }

    /**
     * 热门视频，点赞最多的前五
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> queryByLikeNum() {
        List<Map<String, Object>> hotMapList = new ArrayList<>();
        //打包查询条件
        QueryWrapper<Sort> wrapper = new QueryWrapper<>();
        wrapper.eq("level", 1);
        List<Sort> supSortList = sortMapper.selectList(wrapper);
        for (Sort sort : supSortList) {
            //一级分类ID
            int styleId = sort.getId();
            //分类名称
            String type = sort.getType();
            //根据一级分类ID查询热点视频
            List<Videos> sortList = videoMapper.queryHotVideo(styleId);
            Map<String, Object> hotMap = new HashMap<>();
            hotMap.put("type", type);
            hotMap.put("sortList", sortList);
            hotMapList.add(hotMap);
        }
        return hotMapList;
    }

    @Override
    public Map<String, Object> selectById(int id) {
        return videoMapper.selectVideoById(id);
    }

    @Override
    public Users queryUsers(int id) {
        return queryUsers(id);
    }
}
